﻿--[[
   @brief    WGBannerList, 游戏列表
   @author   harleyxwang
   @version  1.0
   @date     2017-04-21
--]]


base_utility.log("loading banner_list.lua");
ierd_tgp.tpf_ui.Require("wegame_banner\\banner.lua");

HomePageButtonPos = {
    kIconOffsetX = 20; -- 主页按钮x偏移
    kIconWidth = 24; -- 主页按钮图片宽度
    kIconPadding = 10; -- 主页按钮与文本间距
    kIconTextPadding = 7; -- 主页按钮文本与beta按钮间距
    kTextHDPIFixed = 60;
    kTextEnHDPIFixed = 70;
};

WGBannerList = {
    in_refreshing = false;
    game2banner_ = {};
    ctrl2banner_ = {};
    game2config_ = {};   -- 保存banner节点相关的配置，因为banner可能会重新创建而丢失原来的信息
    subgame2banner_ = {};  -- 预约游戏
    ADD_GAME = 0;
    GAME_EXIST = 1;
    home_page_btn_id_ = nil;
    ITEM_COUNT_PER_REFRESH = 10;
};

function WGBannerList:log(msg, core_msg)
    base_utility.log(string.format("[WGBannerList] %s", msg), core_msg);
end

function WGBannerList:attach(ctrl_id, subs_ctrl_id)
    self:log(string.format("attach %d", ctrl_id));
    self.ctrl_id_ = ctrl_id; 
    self.list_ctr_ = ui_utility.get_ctrl_ptr_by_id(ctrl_id);
    self.subscription_list_ctrl_id = subs_ctrl_id;
    self.subscription_list_ctrl_ptr = ui_utility.get_ctrl_ptr_by_id(subs_ctrl_id);
    self:set_VSB_margin("0,0,0,0");
    self:init_scrollbar_event();
    self:init_home_page_btn();
    self:adapt_xone_mode();
    self.feeds_last_readed_ = "0";
    self.refresh_waiting_ = false;  -- 是否有刷新在等待timer中
    self.need_refresh_all_ = false;  -- 需要重新刷新所有节点
end

function WGBannerList:detach()
    self:log("detach");
    self.game2banner_ = {};
    self.ctrl2banner_ = {};
    self.subgame2banner_ = {}; -- 预约列表相关
    self.subctrl2banner_ = {};
    self.list_ctr_ = nil;
    self.subscription_list_ctrl_ptr = nil;
    self.home_page_btn_id_ = nil;
    self.refresh_waiting_ = false;
    self.need_refresh_all_ = false;
    GameTipsMgr.detach();
end

function WGBannerList:init_home_page_btn()
end

function WGBannerList:re_arrange_item_pos()
    self:log(string.format("re_arrange_item_pos"));
    if self.list_ctr_ == nil then
        self:log(string.format("re_arrange_item_pos, list_ctr_ is NIL, may be not attached yet"),
                            true);
        return;
    end
    
    if self.in_refreshing then
        self:log(string.format("[re_arrange_item_pos]return directtly while in refreshing"));
        return;
    end
    
    self.list_ctr_:ReArrangeItemPos();
end

function WGBannerList:re_layout_item_pos()
    self:log(string.format("re_arrange_item_pos"));
    if self.list_ctr_ == nil then
        self:log(string.format("re_arrange_item_pos, list_ctr_ is NIL, may be not attached yet"),
                            true);
        return;
    end
    if self.in_refreshing then
        self:log(string.format("[re_arrange_item_pos]return directtly while in refreshing"));
        return;
    end
    self.list_ctr_:RelayoutItem();
end

function WGBannerList:refresh_list(refresh_all_nodes)
    -- 当前仍有刷新任务等待timer，直接return
    if self.refresh_waiting_ == true and refresh_all_nodes == true then
        self:log(string.format("refresh_list, need refresh all nodes later"));
        self.need_refresh_all_ = true;
        return;
    end
    base_utility.remove_interval("WGBannerListEventListener.on_tick_refresh_game_list");
    if self.list_ctr_ == nil then
        self:log(string.format("refresh_list, list_ctr_ is NIL, may be not attached yet"));
        self.refresh_waiting_ = false;
        return;
    end
    ui_utility.uilib():LockLayout();
    banner_model.last_selected_ctrl_id_ = nil;
    local visible_list = banner_model.visible_game_list;
    local total_count = visible_list.count;
    -- 开始更新
    local cur_count =  self.list_ctr_:GetItemCount();
    self.list_ctr_:BeginUpdate();
    if cur_count == 0 then
        -- 设置列表item布局
        self.game2banner_ = {};
        self.ctrl2banner_ = {};
        self:set_itemlayoutpath();
        self.adding_game_tip_id_ = Tenio.INVALID_CONTROLID;
        --清理gametips list
        GameTipsMgr.clear_all_tips();
    end
    -- 是否正在添加游戏：
    if self.adding_game_tip_id_ ~= nil and self.adding_game_tip_id_ ~= Tenio.INVALID_CONTROLID then
        if cur_count > 0 then
            self.list_ctr_:RemoveItem(0);
        end
        self.adding_game_tip_id_ = Tenio.INVALID_CONTROLID;
        cur_count =  self.list_ctr_:GetItemCount();
    end

    local refresh_all_games = false; 
    self.in_refreshing = true;
    self:log(string.format("refresh_list, visible_list.count=%d, index=%d,last_selected_game_id:%s",
                           total_count, cur_count, tostring(banner_model.last_selected_game_id_)));
    if cur_count < total_count then
        local insert_count = 0;
        if cur_count + self.ITEM_COUNT_PER_REFRESH < total_count then
            insert_count = self.ITEM_COUNT_PER_REFRESH;
        else 
            insert_count = total_count - cur_count;
        end
        -- 增加节点数量
        local last_index = cur_count + insert_count - 1;
        if insert_count > 0 then
            for index = cur_count, last_index do
                self.list_ctr_:InsertItem(index);
            end
        end
    else 
        -- 删除多余的节点
        if cur_count - total_count > 0 then
            for index = cur_count -1, total_count, -1 do
                self.list_ctr_:RemoveItem(index);
            end
            refresh_all_games = true;
        end
        -- 删除已移除游戏信息
        local vis_list = {};
        for index = 0, total_count do
            local game_id = visible_list.game_id_list[index];
            vis_list[game_id] = true;
        end
        for k, v in pairs(self.game2banner_) do
            if nil == vis_list[k] then
                self.game2banner_[k] = nil;
            end
        end
    end
    
    --判断是否是专版
    local gamelib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    local bindid = gamelib:get_bind_game_id();
    local lockbind = gamelib:is_lock_postion();
    if total_count > 0 then
        self:log(string.format("begin set game list data."));
    end
    -- 遍历game_list， 逐一设置数据
    local draggable_list_count = self.list_ctr_:GetItemCount();
    local first_index = cur_count;
    local last_index = draggable_list_count - 1;
    if refresh_all_nodes or refresh_all_games then
        first_index = 0;
    end
    for i = first_index, draggable_list_count - 1 do
        local game_id = visible_list.game_id_list[i];
        self:log(string.format("got my game_id:%d", game_id));
        local base_info = banner_model.get_game_base_info(game_id);
        if base_info ~= nil then
            local cur_index = i;
            local item_id = self.list_ctr_:GetItemRootIDByIdx(cur_index);
            if item_id ~= 0 then
                local banner = WGBanner:new(game_id, item_id, WGBannerType.BANNER_TYPE_NORMAL);
                self.game2banner_[game_id] = banner;
                self.ctrl2banner_[item_id] = banner;
                
                banner:refresh_base_data(base_info);

                if bindid == game_id and lockbind == true  then
                    self.list_ctr_:SetItemDraggable(i, false);
                else
                    self.list_ctr_:SetItemDraggable(i, true);
                end;
                if banner_model.last_selected_game_id_ == game_id then
                    banner:selected_game(true);
                else
                    banner:selected_game(false);
                end
            else
                self:log(string.format("item in list_control is 0, index:%d", i));            
            end;

            -- 这里改成模块间通讯方式
            -- 获取CF的Icon图标，修复游戏弹框中使用
            if popup_wnd_model ~= nil then
                popup_wnd_model.set_game_info(i, game_id, base_info, visible_list.count);
            end;
        else 
            self:log(string.format("base_info is NIL, game_id:%d", game_id));
        end;
    end;

    if total_count > 0 then
        self:log(string.format("end set game list data."));
    end

    self.in_refreshing = false;
    -- 结束更新
    self.list_ctr_:EndUpdate();
    ui_utility.uilib():UnlockLayout(self.ctrl_id_);
    self.refresh_waiting_ = false;
    -- 判断是否需要继续刷新
    if draggable_list_count < total_count then
         self.refresh_waiting_ = true;
         base_utility.add_interval("WGBannerListEventListener.on_tick_refresh_game_list", 400);
    elseif self.need_refresh_all_ then
        self:log(string.format("refresh_list, refresh all nodes again"));
        self.need_refresh_all_ = false;
        WGBannerList:refresh_list(true);
    end
end

function WGBannerList:refresh_subscription_list(subscription_list)

    local count = #subscription_list;
    self:log(string.format("refresh_list, subscription_list.count=%d", count));
    if self.subscription_list_ctrl_ptr == nil then
        self:log(string.format("refresh_list, list_ctr is NIL, may be not attached yet"));
        return;
    end
    
    self.subgame2banner_ = {};
    self.subctrl2banner_ = {};

    self.subscription_list_ctrl_ptr:BeginUpdate();
    self.subscription_list_ctrl_ptr:SetItemCount(count);

    for i = 1, count do
        local game_id = subscription_list[i].gameid or 0;
        self:log(string.format("got subscription game_id:%d", game_id));
        local base_info = banner_model.get_game_base_info(game_id);
        if base_info ~= nil then
            local cur_index = i - 1;
            local item_id = self.subscription_list_ctrl_ptr:GetItemRootIDByIdx(cur_index);
            if item_id ~= 0 then
                local banner = WGBanner:new(game_id, item_id, WGBannerType.BANNER_TYPE_SUBSCRIPTION);
                self.subgame2banner_[game_id] = banner;
                self.subctrl2banner_[item_id] = banner;
                
                banner:refresh_base_data(base_info);

                self.subscription_list_ctrl_ptr:SetItemDraggable(cur_index, false);

                if banner_model.last_selected_game_id_ == game_id then
                    banner:selected_game(true);
                else
                    banner:selected_game(false);
                end
            else
                self:log(string.format("item in list_control is 0, index:%d", i));            
            end;
        else 
            self:log(string.format("base_info is NIL, game_id:%d", game_id));
        end;
    end;

    self.subscription_list_ctrl_ptr:EndUpdate();
end

-- 根据不同模式动态变更列表的ItemLayoutPath
function WGBannerList:set_itemlayoutpath()
    ui_utility.setproperty_by_id(self.ctrl_id_, "ItemLayoutPath", "wegame_bannerlist\\WGBannerWrapper.tpf/TenioUILayout/UIObject");
end

function WGBannerList:refresh_base_data(game_id)
    self:log(string.format("refresh_base_data, game_id=%d", game_id));
    if self.list_ctr_ == nil or self.subscription_list_ctrl_ptr == nil then
        self:log("refresh_base_data, list_ctr_ is NIL, may be not attached yet");
        return;
    end

    if game_id ~= 0 then
        local banner = self.game2banner_[game_id];
        if banner ~= nil then
            banner:refresh_base_data();
        else
            self:log(string.format("[refresh_base_data]banner is NIL, game_id=%d", game_id));
        end;
    else
        for id,banner in pairs(self.game2banner_) do
            if banner ~= nil then
                banner:refresh_base_data();
            else
                self:log(string.format("[refresh_base_data]banner is NIL, game_id=%d", game_id));
            end;
        end
    end;

    -- subscription game list
    if game_id ~= 0 then
        local banner = self.subgame2banner_[game_id];
        if banner ~= nil then
            banner:refresh_base_data();
        else
            self:log(string.format("[refresh_base_data]subbanner is NIL, game_id=%d", game_id));
        end;
    else
        for id,banner in pairs(self.subgame2banner_) do
            if banner ~= nil then
                banner:refresh_base_data();
            else
                self:log(string.format("[refresh_base_data]subbanner is NIL, game_id=%d", game_id));
            end;
        end
    end;
end

function WGBannerList:refresh_state(game_id)
    self:log(string.format("refresh_state, game_id=%d", game_id));
    if self.list_ctr_ == nil or self.subscription_list_ctrl_ptr == nil then
        self:log("refresh_state, list_ctr_ is NIL, may be not attached yet");
        return;
    end
    if game_id ~= 0 then
        local banner = self.game2banner_[game_id];
        if banner ~= nil then
            banner:refresh_state();
        else
            self:log(string.format("[refresh_state]banner is NIL, game_id=%d", game_id));
        end;
    else
        for id,banner in pairs(self.game2banner_) do
            if banner ~= nil then
                banner:refresh_state();
            else
                self:log(string.format("[refresh_state]banner is NIL, game_id=%d", game_id));
            end;
        end
    end;

    if game_id ~= 0 then
        local banner = self.subgame2banner_[game_id];
        if banner ~= nil then
            banner:refresh_state();
        else
            self:log(string.format("[refresh_state]subbanner is NIL, game_id=%d", game_id));
        end;
    else
        for id,banner in pairs(self.subgame2banner_) do
            if banner ~= nil then
                banner:refresh_state();
            else
                self:log(string.format("[refresh_state]subbanner is NIL, game_id=%d", game_id));
            end;
        end
    end;
end

function WGBannerList:refresh_preupdate_state(game_id)
    self:log(string.format("refresh_preupdate_state, game_id=%d", game_id));
    if self.list_ctr_ == nil then
        self:log("refresh_preupdate_state, list_ctr_ is NIL, may be not attached yet");
        return;
    end
    if game_id ~= 0 then
        local banner = self.game2banner_[game_id];
        if banner ~= nil then
            banner:refresh_preupdate_state();
        else
            self:log(string.format("[refresh_preupdate_state]banner is NIL, game_id=%d", game_id));
        end;
    end;
end

function WGBannerList:get_banner(game_id)
    self:log(string.format("get_banner, game_id=%d", game_id));
    if nil ~= self.game2banner_[game_id] then
        return self.game2banner_[game_id];
    end
    return self.subgame2banner_[game_id];
end

-- 根据Banner控件id取banner对象
function WGBannerList:get_banner_by_ctrl(id)
    self:log(string.format("get_banner_by_ctrl, banner_id=%d", id));
    local wrapper_id = ui_utility.get_ctrl_parent_id(id);
    return self:get_banner_by_wrapper(wrapper_id);
end

-- 根据Banner控件id取banner对象
function WGBannerList:get_banner_by_wrapper(id)
    self:log(string.format("get_banner_by_wrapper, wrapper_id=%d", id));
    if nil ~= self.ctrl2banner_[id] then
        return self.ctrl2banner_[id];
    end
    return self.subctrl2banner_[id];
end

function WGBannerList:selected_game(game_id, selected, ctrl_id)
    self:log(string.format("set_game_selected, game_id:%d", game_id));
    if self.list_ctr_ == nil then
        self:log("selected_game, list_ctr_ is NIL, may be not attached yet");
        return;
    end
    local banner = nil;
    if nil ~= ctrl_id then
        local banner_id = ui_utility.get_ctrl_parent_id(ctrl_id);      
        banner = self:get_banner_by_ctrl(banner_id);
    end
    if nil == banner then
        banner = self:get_banner(game_id);
    end
    if nil == banner then
        self:log(string.format("[set_game_selected]banner is NIL, game_id:%d",
                 game_id), true);
        return;
    end

    banner:selected_game(selected);
    if nil == selected or false == selected then
        return;
    end

    if banner:is_subscription_banner() then
        for k, v in pairs(self.subgame2banner_) do
            if nil ~= k and nil ~= v and game_id ~= k then
                v:selected_game(false);
            end
        end
    else
        for k, v in pairs(self.game2banner_) do
            if nil ~= k and nil ~= v and game_id ~= k then
                v:selected_game(false);
            end
        end
    end
end

function WGBannerList:change_selected_game(game_id, ctrl_id)
    self:log(string.format("change_selected_game, game_id:%d, last_selected_game_id:%s",
                    game_id, tostring(banner_model.last_selected_game_id_)));
    

    if banner_model.last_selected_game_id_ == game_id then
        self:log(string.format("[change_selected_game]last_selected_game_id_ is some as " ..
                              " game_id:%d, do not change", game_id));
        return;
    end
    -- 首先反选上次选中的节点
    if banner_model.last_selected_game_id_~= nil and banner_model.last_selected_game_id_ ~= 0 then
        if banner_model.last_selected_game_id_ == GAME_ID.GAEM_ID_HOME_PAGE then
            self:select_home_page_btn(false);
        else
            self:selected_game(banner_model.last_selected_game_id_, false, banner_model.last_selected_ctrl_id_);
        end
    else
        self:log(string.format("[change_selected_game]last_selected_game_id_ is NIL or 0, game_id:%d",
           game_id));
    end
    -- 选中新的节点
    if game_id ~= 0 then
        if game_id == GAME_ID.GAEM_ID_HOME_PAGE then
            self:select_home_page_btn(true);
        else
            self:selected_game(game_id, true, ctrl_id);
        end
        banner_model.last_selected_game_id_ = game_id;
    end

    banner_model.last_selected_ctrl_id_ = ctrl_id;
end

function WGBannerList:show_adding_game()
    self:log(string.format("show_adding_game"));
    
    if self.list_ctr_ == nil then
        self:log(string.format("[show_adding_game]list_ctr_ is NIL, may be not attached yet"), true);
        return;
    end
    
    banner_control.ensure_my_game_list_show();

    if self.adding_game_tip_id_== nil or self.adding_game_tip_id_ == Tenio.INVALID_CONTROLID then
        local str, str_hoder = U8toCUIStr("wegame_bannerlist\\GameDragAddTips.tpf/TenioUILayout/UIObject");
        local adding_id = self.list_ctr_:InsertItem(0, str);
        
        self:log(string.format("[show_adding_game]InsertItem returned %d", adding_id));
        self.adding_game_tip_id_ = adding_id;
    end
    -- 显示添加图标和文本
    ui_utility.setproperty_by_path(self.adding_game_tip_id_, "StateIcon", 
        "Background", "ONEIMAGE:bottom_bar\\bottom_bar.png?146,173,166,193;Center;0;"); 
    ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
        "TextColor", "RGB:255,255,255");
    ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
        "Alpha", "102");
    ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
         "Caption", LoadStr(ID_ADDING_GAME_DROG_TO));
end

function WGBannerList:remove_adding_game()
    self:log(string.format("remove_adding_game"));

    if self.list_ctr_ == nil then
        self:log("[remove_adding_game] list_ctr_ is NIL, may be not attached yet");
        return;
    end
    
    if self.adding_game_tip_id_ == nil or self.adding_game_tip_id_ == Tenio.INVALID_CONTROLID then
        self:log(string.format("[remove_adding_game]adding_game_tip_id_ is INVALID_CONTROLID"),
             true);
        return;
    end
    
    if self.list_ctr_:RemoveItem(0) then
        self.adding_game_tip_id_ = Tenio.INVALID_CONTROLID;
    else
        self:log(string.format("[remove_adding_game]RemoveItem returned FAIL"), true);
    end
end

function WGBannerList:add_game_result(result_param)
    self:log(string.format("[add_game_result]add_game_result"));
    
    if self.list_ctr_ == nil then
        self:log("[add_game_result] list_ctr_ is NIL, may be not attached yet");
        return;
    end
    
    if result_param == nil then
        self:log(string.format("[add_game_result]param is NIL"), true);
        return;
    end
    if result_param.result == nil then
        self:log(string.format("[add_game_result]result_param.result is NIL"), true);
        return;
    end
    
    if result_param.result == ierd_tgp.game_library.SUCCESS then
         self:remove_adding_game();
    elseif result_param.result == ierd_tgp.game_library.GAME_EXIST then
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "StateIcon", 
            "Background", "ONEIMAGE:bottom_bar\\bottom_bar.png?146,201,166,221;Center;0;"); 
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
            "TextColor", "RGB:246,44,57");
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
            "Alpha", "255");
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
            "Caption", LoadStr(ID_ADDING_GAME_GAME_EXIST));
    elseif result_param.result == ierd_tgp.game_library.INVALID or
        result_param.result == ierd_tgp.game_library.PATH_ILLEGAL then
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "StateIcon", 
            "Background", "ONEIMAGE:bottom_bar\\bottom_bar.png?146,201,166,221;Center;0;"); 
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
            "TextColor", "RGB:246,44,57");
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
            "Alpha", "255");
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
            "Caption", LoadStr(ID_ADDING_GAME_GAEM_UNKNOW));
    elseif result_param.result == ierd_tgp.game_library.WAITING_INFO then
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "StateIcon", 
            "Background", "ONEIMAGE:bottom_bar\\bottom_bar.png?146,173,166,193;Center;0;"); 
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
            "TextColor", "RGB:255,255,255");
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
            "Alpha", "102");
        ui_utility.setproperty_by_path(self.adding_game_tip_id_, "ResultTxt",
            "Caption", LoadStr("BANNER_ACTION_WAITING_DOWNLOAD_CONFIG"));   
    end
    if result_param.result ~= ierd_tgp.game_library.SUCCESS then
         WGBannerListEventListener.add_tick();
    end
end

function WGBannerList:on_tick()
    self:log(string.format("[on_tick]tip showing timeout, hide adding tips"));
    self:remove_adding_game();
    WGBannerListEventListener.remove_tick();
end

function WGBannerList:set_draggable(index, is_draggable)
    if self.list_ctr_ then
        self.list_ctr_:SetItemDraggable(index, is_draggable);
    end
end

function WGBannerList:set_VSB_stay(is_stay)
    self:log(string.format("[set_VSB_Stay]is_stay:%s", tostring(is_stay)));
    if nil ~= self.ctrl_id_ then
        local stay_str = "0";
        if is_stay then
            stay_str = "1";
        end
        ui_utility.setproperty_by_id(self.ctrl_id_, "VScrollBarIsStay", stay_str);
    end
end

function WGBannerList:set_VSB_margin(margin_str)
    self:log(string.format("[set_VSB_margin]margin:%s", margin_str));
    if nil ~= self.ctrl_id_ then
        ui_utility.setproperty_by_id(self.ctrl_id_, "VScrollBarMargin", margin_str);
    end
end

function WGBannerList:get_current_select_module()
    return banner_model.last_selected_game_id_;
end

function WGBannerList:is_item_begin_dragged()
    local is_dragged = false;
    if nil ~= self.list_ctr_ then
        is_dragged = self.list_ctr_:IsItemBeingDragged();
    end
    return is_dragged;
end

function WGBannerList:scroll_to_top()
    if self.list_ctr_ == nil then
        self:log("[scroll_to_top] list_ctr_ is NIL, may be not attached yet");
        return;
    end
    self.list_ctr_:ScrollToTop();
end

function WGBannerList:select_home_page_btn(is_select) 
    if is_select then
        ui_utility.setproperty_by_id(self.home_page_btn_id_, "Background", 
            "ONEIMAGE:banner_list\\banner_list.png?10,170,199,180;Stretch;0;");
--        ui_utility.setproperty_by_id(self.home_page_btn_id_, "Icon", 
--            "banner_list\\banner_list.png?44,10,68,34;|banner_list\\banner_list.png?44,10,68,34;|banner_list\\banner_list.png?44,10,68,34;");
        ui_utility.setproperty_by_id(self.home_page_btn_id_, "TextColor", self.text_highlight_color_);
        ui_utility.setproperty_by_id(self.home_page_btn_id_, "TextColorHover", self.text_highlight_color_);
        ui_utility.setproperty_by_id(self.home_page_btn_id_, "TextColorPressed", self.text_highlight_color_);

        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        --tpfui:RemoveInterval("WGBannerList:query_feeds_unread_count_timer");   
    else
        ui_utility.setproperty_by_id(self.home_page_btn_id_, "Background", "");
--        ui_utility.setproperty_by_id(self.home_page_btn_id_, "Icon", 
--            "banner_list\\banner_list.png?44,10,68,34;|banner_list\\banner_list.png?44,10,68,34;|banner_list\\banner_list.png?44,10,68,34;");
        ui_utility.setproperty_by_id(self.home_page_btn_id_, "TextColor", "RGB:255,255,255");
        ui_utility.setproperty_by_id(self.home_page_btn_id_, "TextColorHover", "RGB:255,255,255");
        ui_utility.setproperty_by_id(self.home_page_btn_id_, "TextColorPressed", "RGB:255,255,255");

        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        --tpfui:AddInterval("WGBannerList:query_feeds_unread_count_timer", 5 * 60 * 1000);
    end
end

function WGBannerList:query_feeds_unread_count_timer()
end

function WGBannerList:feeds_unread_count_rsp(param_tbl)
end

function WGBannerList:feeds_set_last_readed(param_tbl)
    if param_tbl ~= nil and param_tbl.last_readed ~= nil and param_tbl.feeds_type ~= nil then
        if tonumber(param_tbl.last_readed) ~= nil and tonumber(param_tbl.last_readed) >= 0 and param_tbl.feeds_type == "timeline" then
            self.feeds_last_readed_ = tostring(param_tbl.last_readed);
        end
    end
end

function WGBannerList:set_view_bule_point(game_id, has_view_bule_point)
    if WGBannerList.game2config_[game_id] == nil then
        WGBannerList.game2config_[game_id] = {};
    end
    WGBannerList.game2config_[game_id].view_bule_point = has_view_bule_point;
end

function WGBannerList:get_view_bule_point(game_id)
    if WGBannerList.game2config_[game_id] == nil then
        return false;
    else 
        return WGBannerList.game2config_[game_id].view_bule_point;
    end
end

function WGBannerList:set_prev_game_state(game_id, state)
    if WGBannerList.game2config_[game_id] == nil then
        WGBannerList.game2config_[game_id] = {};
    end
    WGBannerList.game2config_[game_id].prev_game_state = state;
end

function WGBannerList:get_prev_game_state(game_id, state)
    if WGBannerList.game2config_[game_id] == nil then
        return 0;
    end
    return WGBannerList.game2config_[game_id].prev_game_state or 0;
end

function WGBannerList:set_showing_bule_point(game_id, is_showing)
    if WGBannerList.game2config_[game_id] == nil then
         WGBannerList.game2config_[game_id] = {};
    end
    WGBannerList.game2config_[game_id].showing_bule_point = is_showing;
end

function WGBannerList:get_is_showing_bule_point(game_id)
    if WGBannerList.game2config_[game_id] == nil then
        return false;
    else 
        return WGBannerList.game2config_[game_id].showing_bule_point;
    end
end

function WGBannerList:get_banner_by_type(banner_type)
    if 0 == banner_type then
        return self.game2banner_;
    end
    return self.subgame2banner_;
end

function WGBannerList:init_scrollbar_event()
    if nil == self.list_ctr_ or nil == self.subscription_list_ctrl_ptr then
        return;
    end
    self.subs_list_scrollbar_show_ = false;
    self.list_scrollbar_show_ = false;
    local MAX_NAME = 260;
    local ui_str, mem_holder = NewUIStr(MAX_NAME);
    self.list_ctr_:GetControlName(ui_str, MAX_NAME);
    local name = UIStrtoU8(ui_str);
    name = name.."VScrollVert";
    local scrollbar_id = ui_utility.get_ctrl_id_by_path(self.list_ctr_:GetControlID(), name);
    if Tenio.INVALID_CONTROLID ~= scrollbar_id then
        ui_utility.setproperty_by_id(scrollbar_id, "OnShow", 
                                                "WGBannerListEventListener.on_scrollbar_show");
        ui_utility.setproperty_by_id(scrollbar_id, "OnHide", 
                                                "WGBannerListEventListener.on_scrollbar_hide");
    end

    self.subscription_list_ctrl_ptr:GetControlName(ui_str, MAX_NAME);
    local name = UIStrtoU8(ui_str);
    name = name.."VScrollVert";
    scrollbar_id = ui_utility.get_ctrl_id_by_path(self.list_ctr_:GetControlID(), name);
    if Tenio.INVALID_CONTROLID ~= scrollbar_id then
        ui_utility.setproperty_by_id(scrollbar_id, "OnShow", 
                                                "WGBannerListEventListener.on_subs_scrollbar_show");
        ui_utility.setproperty_by_id(scrollbar_id, "OnHide", 
                                                "WGBannerListEventListener.on_subs_scrollbar_hide");
    end

end

function WGBannerList:set_scrollbar_show(list_type, show)
    if 0 == list_type then
        self.list_scrollbar_show_ = show;
    else
        self.subs_list_scrollbar_show_ = show;
    end
end

function WGBannerList:is_list_scrollbar_show(list_type)
    if 0 == list_type then
        return self.list_scrollbar_show_;
    end
    return self.subs_list_scrollbar_show_;
end

function WGBannerList:adapt_xone_mode()
    local device_xone = comm_center.call_service("Svr_LoginMgr_IsDeviceXOne");
    if device_xone ~= nil and device_xone.is_xone ~= nil and device_xone.is_xone ~= 0 then
        self.list_ctr_:EnableDrag(0);
    end;
end
---------------------------------------------------------------------------------------------------
-- 
---------------------------------------------------------------------------------------------------
WGBannerListEventListener = {
    initialed_ = false;
};

function WGBannerListEventListener.init()
    if initialed_ then
        return;
    end;
    
end;

function WGBannerListEventListener.add_tick()
    base_utility.log(string.format("[WGBannerListEventListener]add_tick"));
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval("WGBannerListEventListener.on_tick", 1 * 1000);
end;

function WGBannerListEventListener.remove_tick()
    base_utility.log(string.format("[WGBannerListEventListener]remove_tick"));
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval("WGBannerListEventListener.on_tick");   
end;

function WGBannerListEventListener.on_adding_game(event_id)
    base_utility.log(string.format("[WGBannerListEventListener]on_adding_game"));
    WGBannerList:show_adding_game();
end;

function WGBannerListEventListener.on_adding_game_cancel(event_id)
    base_utility.log(string.format("[WGBannerListEventListener]on_adding_game_cancel"));
    WGBannerList:remove_adding_game();
end;

function WGBannerListEventListener.on_add_game_result(event_id, json_str)
    base_utility.log(string.format("[WGBannerListEventListener]on_add_game_result, json_str:%s",
                               json_str));
    
    local param = base_utility.json_to_table(json_str);
    WGBannerList:add_game_result(param);
end;

function WGBannerListEventListener.on_tick()
    WGBannerList:on_tick();
end;

function WGBannerListEventListener.on_tick_refresh_game_list()
    WGBannerList:refresh_list(false);
end



---------------------------------------------------------------------------------------------------
-- 
---------------------------------------------------------------------------------------------------
WGBannerListUIListener = {
};

function WGBannerListUIListener.on_drag_enter(uilib, id)
    base_utility.log(string.format("[WGBannerListUIListener]on_drag_enter, id:%d", id));
    WGBannerList:show_adding_game();
end

function WGBannerListUIListener.on_drag_leave(uilib, id)
    base_utility.log(string.format("[WGBannerListUIListener]on_drag_leave, id:%d", id));
    WGBannerList:remove_adding_game();
end

function WGBannerListUIListener.on_drag_drop(uilib, id, file_path)
    base_utility.log(string.format("[WGBannerListUIListener]on_drag_drop, id:%d, path:%s", id, file_path));
    local gamelib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    if nil == gamelib then
        return;
    end

    local result_param = ierd_tgp.game_library.AddGameResultParam:new();
    gamelib:add_installed_game(file_path, result_param);
    if nil ~= result_param then
        local game_id = result_param.game_id;

        local is_netbar = gamelib:get_bind_channel_id();
        if 1 == is_netbar then
            -- 网吧版本
            local is_in_visible_list = false;
            for i = 0, banner_model.visible_game_list.count - 1 do
                if game_id == banner_model.visible_game_list.game_id_list[i] then
                    is_in_visible_list = true;
                    break;
                end
            end
            local param = {};
            if WGBannerList.GAME_EXIST == result_param.result and false == is_in_visible_list then
                -- 游戏已经安装而且不在可见列表，则添加到可见列表
                gamelib:add_or_remove_game(game_id, true);
                param = {result = WGBannerList.ADD_GAME};
            else
                param = {result = result_param.result};
            end
            WGBannerList:add_game_result(param);
        else
            local is_in_visible_list = gamelib:IsInMyVisibleGameList(game_id);
            local param = {};
            if WGBannerList.GAME_EXIST == result_param.result and false == is_in_visible_list then
                -- 游戏已经安装而且不在可见列表，则添加到可见列表
                gamelib:add_or_remove_game(game_id, true);
                param = {result = WGBannerList.ADD_GAME};
            else
                param = {result = result_param.result};
            end
            WGBannerList:add_game_result(param);
        end
        if result_param.result == ierd_tgp.game_library.MULTI_MATCH then
            event_center.send_event(banner_event.ADD_GAME_CONFLICT, base_utility.table_to_json({path = file_path}));
        end;
        result_param:delete();
    end
end

function WGBannerListUIListener.on_order_changed(uilib, id, drage_move)
    base_utility.log(string.format("[WGBannerListUIListener]on_order_changed, id:%d, drage_move:%s",
                             id, tostring(drage_move)));
    if not drage_move then
        base_utility.log(string.format("[WGBannerListUIListener]NOT drage_move, DISCARD, id:%d", id));
        return;
    end
    local list_ptr = ui_utility.get_ctrl_ptr_by_id(id);
    if list_ptr == nil then
        base_utility.log(string.format("[WGBannerListUIListener]list_ptr is NIL,  id:%d", id), true);
        return;
    end
    
    local count = list_ptr:GetItemCount();
    if count <= 0 then
        base_utility.log(string.format("[WGBannerListUIListener]count is 0, DISCARD, id:%d", id), true);
        return;
    end
    
    if count > ierd_tgp.GAME_LIB_MAX_GAME_COUNT then
        base_utility.log(string.format("[WGBannerListUIListener]count is TOO LARGE, count:%d",
                                   count), true);
        count = ierd_tgp.GAME_LIB_MAX_GAME_COUNT;
    end
    
    local game_list = ierd_tgp.game_library.Game_list:new();
    game_list.count = count;
    local game_order = "";
    for i = 0, count - 1 do
        local wrapper_id = list_ptr:GetItemRootIDByIdx(
            i + 0);
        local banner = WGBannerList:get_banner_by_wrapper(wrapper_id);
        if banner then
            game_list.game_id_list[i] = banner.game_id_;
            game_order = game_order .. "|" .. banner.game_id_;
        else
            base_utility.log(string.format("[WGBannerListUIListener]banner is NIL, id:%d",
                                       id), true);
        end 
    end
    banner_model.op_from_my_app_list = true;
    base_utility.log(string.format("[WGBannerListUIListener]order games[count%d]:%s",
                               count, game_order));
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    game_lib:change_game_order(game_list);
    game_list:delete();
end

function WGBannerListUIListener.on_home_page_click(uilib, control_id)
    local param = { };
    param.module_id = GAME_ID.GAEM_ID_HOME_PAGE;
    event_center.send_event(banner_event.JUMP_MODULE_WEB, base_utility.table_to_json(param));
end

function WGBannerListUIListener.on_click_enter_store_btn(uilib, control_id)
    local param = { };
    param.module_id = GAME_ID.GAME_ID_SHOP;
    event_center.send_event(banner_event.JUMP_MODULE_WEB, base_utility.table_to_json(param));
end

function WGBannerListUIListener.on_more_menu_btn_click(uilib, control_id)
    ierd_tgp.tpf_ui.Require("wegame_banner\\game_mgr_menu.lua");
    local rc = nil;
    local control_ptr = ui_utility.get_ctrl_ptr_by_id(control_id);
    if control_ptr ~= nil then
        rc = control_ptr:GetRectEx(TRUE,TRUE);
    end
   
    if rc ~= nil then
         game_mgr_menu.show(rc.left, rc.top);
    end
end

function WGBannerListUIListener.on_banner_mouse_hover(args)
    --体验不佳，暂禁用
    --[[
    local ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.dialog_id);
    if nil ~= ptr then
        ptr:SetFocus();
    end
    --]]
end

function WGBannerListUIListener.on_banner_mouse_leave(args)

end

function WGBannerListEventListener.on_scrollbar_show()
    WGBannerList:set_scrollbar_show(0, true);
    local banners = WGBannerList:get_banner_by_type(0);
    for k,v in pairs(banners) do
        if v.in_hover_ or v.selected_ then
            v:on_scrollbar_show(true);
        end
    end
end

function WGBannerListEventListener.on_scrollbar_hide()
    WGBannerList:set_scrollbar_show(0, false);
    local banners = WGBannerList:get_banner_by_type(0);
    for k,v in pairs(banners) do
        if v.in_hover_ or v.selected_ then
            v:on_scrollbar_show(false);
        end
    end
end

function WGBannerListEventListener.on_subs_scrollbar_show()
    WGBannerList:set_scrollbar_show(1, true);
    local banners = WGBannerList:get_banner_by_type(1);
    for k,v in pairs(banners) do
        if v.in_hover_ or v.selected_ then
            v:on_scrollbar_show(true);
        end
    end
end

function WGBannerListEventListener.on_subs_scrollbar_hide()
    WGBannerList:set_scrollbar_show(1, false);
    local banners = WGBannerList:get_banner_by_type(1);
    for k,v in pairs(banners) do
        if v.in_hover_ or v.selected_ then
            v:on_scrollbar_show(false);
        end
    end
end
------------------------------

